package VisaoControleAcademico;


import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MaskFormatter;

import ControleAcamico.ControlePrincipal;
import ControleAcamico.RegistroAulasControlador;
import ControleDiversas.RegistroAulasDao;
import ModeloAcademico.RegistroAulasBeen;




public class TelaRegistroAulas extends JFrame implements ViewRA{
	
	public TelaRegistroAulas(RegistroAulasControlador controleRA) {
		this.controleRA = controleRA;
		setLayout(null);
		inicializaCampos();
		adiconaComponentesNaTela();
		editaPosicaoDosComponentes();
		adicionaAcao();
		setTitle("Registro de Aulas");
		setSize(540,480);
		setLocationRelativeTo(null);
		setVisible(true);
		
		tabelaDeRegistrosAulas.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		lsregistroaulas = tabelaDeRegistrosAulas.getSelectionModel();
		lsregistroaulas.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
			if(! e.getValueIsAdjusting()){
				linhaSelecionada(tabela);
			}	
			}
		});
	}

	/**
	 * --------------------Registro de Aulas--------------------------
	 */
	private RegistroAulasControlador controleRA;
	
	private static final long serialVersionUID = 1L;
	private JLabel lbProfessor;
	private JLabel lbTurma;
	private JLabel lbCurso;
	private JLabel lbData;
	private JLabel lbConteudo;

	private JComboBox jcProfessor, jcTurma, jcCurso;
	private JFormattedTextField tfdata;
	private JTextArea taConteudo;

	private JButton  registrar;
	private JButton  cancelar;
	private JButton  limpar;
	private JButton  pesquisar;

	private JTable  tabela;
	private JTable tabelaDeRegistrosAulas;
	DefaultTableModel modeloTabela;
	private JScrollPane  scoller;
	private ListSelectionModel lsregistroaulas ;
	
	
	public JComboBox getJcProfessor() {
		return jcProfessor;
	}

	public void setJcProfessor(JComboBox jcProfessor) {
		this.jcProfessor = jcProfessor;
	}

	public JComboBox getJcTurma() {
		return jcTurma;
	}

	public void setJcTurma(JComboBox jcTurma) {
		this.jcTurma = jcTurma;
	}


	public JComboBox getJcCurso() {
		return jcCurso;
	}

	public void setJcCurso(JComboBox jcCurso) {
		this.jcCurso = jcCurso;
	}

	public JFormattedTextField getTfdata() {
		return tfdata;
	}

	public void setTfdata(JFormattedTextField tfdata) {
		this.tfdata = tfdata;
	}

	public JTextArea getTaConteudo() {
		return taConteudo;
	}

	public void setTaConteudo(JTextArea taConteudo) {
		this.taConteudo = taConteudo;
	}
	
	
	

	private void adiconaComponentesNaTela() {
		add(lbProfessor);
		add(lbTurma);
		add(lbCurso);
		add(lbData);
		add(lbConteudo);

		add(jcProfessor);
		add(jcTurma);
		add(jcCurso);
		add(tfdata);
		add(taConteudo);

		add(registrar);
		add(cancelar);
		add(limpar);
		add(pesquisar);
		add(scoller);		
	}

	private void editaPosicaoDosComponentes() {
		lbProfessor            .setBounds(20, 40, 80, 20);  
		lbTurma                .setBounds(20, 80, 89, 20);
		lbCurso                .setBounds(20, 120, 100, 20);
		lbData                 .setBounds(20, 160, 60, 20);
		lbConteudo             .setBounds(20, 200, 89, 20);
		jcProfessor            .setBounds(110, 40, 400, 20);
		jcTurma                .setBounds(110, 80, 400, 20);
		jcCurso                .setBounds(110, 120, 400, 20);
		tfdata                 .setBounds(110, 160, 100, 20);
		taConteudo             .setBounds(110, 200, 400, 60);  
		registrar              .setBounds(5, 280, 120, 30);
		limpar                 .setBounds(135, 280, 120, 30);
		cancelar               .setBounds(265, 280, 120, 30);
		pesquisar              .setBounds(395, 280, 120, 30);
		scoller                .setBounds(2, 320, 510, 100);
	}

	private void inicializaCampos() {
		lbProfessor         = new JLabel("PROFESSOR.:");
		lbTurma             = new JLabel("TURMA.:");
		lbCurso             = new JLabel("CURSO.:");
		lbData              = new JLabel("DATA.:");
		lbConteudo          = new JLabel("CONTEUDO.:");
		
		jcProfessor	    	= new JComboBox();
		jcTurma				= new JComboBox();
		jcCurso				= new JComboBox();
		try {
			tfdata      	 = new JFormattedTextField(new MaskFormatter("##/##/####"));
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		taConteudo        	= new JTextArea(40,40);
		
		registrar         	= new JButton("REGISTRAR");
		cancelar          	= new JButton("CANCELAR");
		limpar            	= new  JButton("LIMPAR");
		pesquisar 	        = new JButton("PESQUISAR");
		
		modeloTabela      	= new DefaultTableModel(null,new String[]{"Professor","Turma","Curso","Data","Conteudo"});
		tabelaDeRegistrosAulas    = new JTable(modeloTabela);
		scoller             = new JScrollPane(tabelaDeRegistrosAulas);
	}
	
	public JTable getTabelaDeRegistrosAulas() {
		return tabelaDeRegistrosAulas;
	}

	public void adicionaAcao(){
		registrar               .setActionCommand("REGISTRAR");
		registrar               .addActionListener(controleRA);
		limpar                  .setActionCommand("LIMPAR");
		limpar                  .addActionListener(controleRA);
		cancelar                .setActionCommand("CANCELAR");
		cancelar                .addActionListener(controleRA);
		pesquisar               .setActionCommand("PESQUISAR");
		pesquisar 				.addActionListener(controleRA);
	}

	@Override
	public void fecha() {
		dispose();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	@Override
	public void mostra() {
		setVisible(true);
		
	}


	public void mostraPesqisa(List<RegistroAulasBeen> registroaulas) {
		while(modeloTabela.getRowCount()>0){
			modeloTabela.removeRow(0);
		}
		if(registroaulas.size()==0){
			JOptionPane.showMessageDialog(null, "Nenhum contato na agenda");
		}else{
			String[]linha=new String[]{null,null,null,null,null,null};
			for(int i= 0; i<registroaulas.size();i++){
				modeloTabela.addRow(linha);
				modeloTabela.setValueAt(registroaulas.get(i).getProfessor(), i, 0);
				modeloTabela.setValueAt(registroaulas.get(i).getTurma(), i, 1);
				modeloTabela.setValueAt(registroaulas.get(i).getCurso(), i, 2);
				modeloTabela.setValueAt(registroaulas.get(i).getData(), i, 3);
				modeloTabela.setValueAt(registroaulas.get(i).getConteudo(), i, 4);
			}
		}
	}
	
	public void linhaSelecionada(JTable tabela){
		try {	
			RegistroAulasDao f = new RegistroAulasDao();
			List<RegistroAulasBeen> oi = f.pesquisar();
			if(tabelaDeRegistrosAulas.getSelectedRow()!= -1){
			jcProfessor.setSelectedItem(oi.get(tabelaDeRegistrosAulas.getSelectedRow()).getProfessor());
			jcTurma.setSelectedItem(oi.get(tabelaDeRegistrosAulas.getSelectedRow()).getTurma());
			jcCurso.setSelectedItem(oi.get(tabelaDeRegistrosAulas.getSelectedRow()).getCurso());
			tfdata.setText(oi.get(tabelaDeRegistrosAulas.getSelectedRow()).getData().trim());
			taConteudo.setText(oi.get(tabelaDeRegistrosAulas.getSelectedRow()).getConteudo().trim());
			}else{
				jcProfessor     .setSelectedItem("");
				jcTurma    		.setSelectedItem("");
				jcCurso       	.setSelectedItem("");
				tfdata    		.setText("");
				taConteudo      .setText("");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}	
	}	
}
